package com.headcaselabs.lb.common;

import java.io.Serializable;
import java.util.Date;

/* 
Copyright 2007, HeadCase Humanufacturing, Inc.

	Licensed under the Apache License, Version 2.0 (the "License"); you
	may not use this file except in compliance with the License. You may
	obtain a copy of the License at
	
	   http://www.apache.org/licenses/LICENSE-2.0
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
	implied. See the License for the specific language governing
	permissions and limitations under the License.
*/
/**
 * A data structure that represents a server node.
 * @author Florian Leibert
 */
public class Server implements Serializable {

	private static final long	serialVersionUID	= 5949559751764014855L;
	private String fqdn;
	private String hostname;
	private String ip;
	private String recordType;
	private long modified;
	private long selectionCount;
	
	/**
	 * Default Constructor
	 * @param recordType
	 */
	public Server(String recordType) {
		this.recordType = recordType;
		selectionCount = 0;
		modified = (new Date()).getTime();
	}
	
	
	/**
	 * Full Constructor
	 * @param recordType 
	 * @param arg
	 * @param hostname
	 */
	public Server(String recordType, String arg, String hostname) {
		selectionCount = 0;
		if (recordType.equals("A")) {
			ip = arg;
		} else if (recordType.equals("CNAME")) {
			fqdn = arg;
		} else {
			throw new RuntimeException("Invalid recordType specified");
		}
		this.hostname = hostname;
		this.recordType = recordType;
		modified = (new Date()).getTime();
	}
	
	/**
	 * @return the hostname
	 */
	public String getHostname() {
		return hostname;
	}

	/**
	 * @param hostname
	 */
	public void setHostname(String hostname) {
		this.hostname = hostname;
	}

	/**
	 * TODO: implement selection count
	 * @return how often the server has been selected.
	 */
	public long getSelectionCount() {
		return selectionCount;
	}

	/**
	 * @param selected
	 */
	public void setSelectionCount(long selected) {
		this.selectionCount = selected;
	}

	//TODO: Discuss if merging FQDN & IP into arg makes sense, maybe both are useful in the future???
	/**
	 * @return the fully qualified domain name
	 */
	public String getFqdn() {
		return fqdn;
	}

	/**
	 * @param fqdn
	 */
	public void setFqdn(String fqdn) {
		this.fqdn = fqdn;
	}
	
	/**
	 * @return the ip
	 */
	public String getIp() {
		return ip;
	}

	/**
	 * @param ip the ip to set
	 */
	public void setIp(String ip) {
		this.ip = ip;
	}

	/**
	 * @return the recordType
	 */
	public String getRecordType() {
		return recordType;
	}

	/**
	 * @param recordType the recordType to set
	 */
	public void setRecordType(String mode) {
		this.recordType = mode;
	}

	@Override
	public int hashCode() {
		final int PRIME = 31;
		int result = 1;
		result = PRIME * result + ((fqdn == null) ? 0 : fqdn.hashCode());
		result = PRIME * result + ((hostname == null) ? 0 : hostname.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final Server other = (Server) obj;
		if (fqdn == null) {
			if (other.fqdn != null)
				return false;
		} else if (!fqdn.equals(other.fqdn))
			return false;
		if (hostname == null) {
			if (other.hostname != null)
				return false;
		} else if (!hostname.equals(other.hostname))
			return false;
		return true;
	}

	/**
	 * @return the modified
	 */
	public long getModified() {
		return modified;
	}

	/**
	 * @param modified the modified to set
	 */
	public void setModified(long modified) {
		this.modified = modified;
	}



	/**
	 * @generated by CodeSugar http://sourceforge.net/projects/codesugar */
	
	public String toString() {
		StringBuffer buffer = new StringBuffer();
		buffer.append("[Server:");
		buffer.append(" fqdn: ");
		buffer.append(fqdn);
		buffer.append(" hostname: ");
		buffer.append(hostname);
		buffer.append(" ip: ");
		buffer.append(ip);
		buffer.append(" recordType: ");
		buffer.append(recordType);
		buffer.append(" modified: ");
		buffer.append(modified);
		buffer.append(" selectionCount: ");
		buffer.append(selectionCount);
		buffer.append("]");
		return buffer.toString();
	}




}
